# Contributor: Jeff Bilyk <jbilyk@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=smokeping
pkgver=2.8.2
pkgrel=3
pkgdesc="Smokeping network latency monitoring"
pkgusers="smokeping"
pkggroups="smokeping"
install="$pkgname.pre-install"
url="https://oss.oetiker.ch/smokeping/"
options="!check"  # No test suite
source="https://oss.oetiker.ch/smokeping/pub/smokeping-$pkgver.tar.gz
	smokeping.initd
	musl-lc_numeric-utf8.patch
	smokeping-config.patch
	smokeping-silence.patch
	smokeping-2.8.2-no-3rd-party.patch
	smokeping-2.8.2-paths.patch
	"
depends="
	font-dejavu
	fping
	perl
	perl-cgi
	perl-cgi-fast
	perl-cgi-session
	perl-config-grammar
	perl-data-hexdump
	perl-digest-hmac
	perl-fcgi
	perl-io-socket-ssl
	perl-io-tty
	perl-ldap
	perl-libwww
	perl-mozilla-ca
	perl-net-dns
	perl-net-ip
	perl-net-openssh
	perl-net-snmp
	perl-net-telnet
	perl-text-soundex
	perl-rrd
	perl-snmp-session
	perl-uri
	rrdtool
	"
makedepends="
	autoconf
	automake
	openssl-dev>3
	perl-dev
	perl-try-tiny
	zlib-dev
	"
arch="noarch !riscv64"
license="GPL-2.0-or-later"
subpackages="$pkgname-doc $pkgname-openrc"

_vendorlib=/usr/share/perl5/vendor_perl

prepare() {
	default_prepare

	# provided by perl-snmp-session
	rm -r lib/BER.pm lib/SNMP_Session.pm lib/SNMP_util.pm
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--bindir=/usr/bin \
		--libdir=/usr/lib \
		--sysconfdir=/etc/smokeping \
		--libexecdir=/usr/libexec \
		--datarootdir=/usr/share \
		--mandir=/usr/share/man \
		--with-htdocs-dir=/usr/share/webapps/smokeping

	make DESTDIR="$pkgdir"
}

package() {
	make DESTDIR="$pkgdir" install

	for i in basepage.html config smokemail smokeping_secrets tmail; do
		mv "$pkgdir"/etc/smokeping/$i.dist "$pkgdir"/etc/smokeping/$i
	done

	# Next part here cleans up the mess that the Makefile leaves behind...

	# Install cgi in the right spot and fix permissions
	mv "$pkgdir"/usr/share/webapps/smokeping/smokeping.fcgi.dist \
		"$pkgdir"/usr/share/webapps/smokeping/smokeping.cgi
	chmod 755 "$pkgdir"/usr/share/webapps/smokeping/smokeping.cgi

	# Fix path to config
	sed -e 's:etc/config:etc/smokeping/config:' \
		-i "$pkgdir"/usr/bin/smokeinfo

	sed -e 's:etc/config.dist:etc/smokeping/config:' \
		-i "$pkgdir"/usr/bin/tSmoke

	# Create softlink for image directory
	ln -s /var/lib/smokeping/images/ "$pkgdir"/usr/share/webapps/smokeping/img

	sed -e '/^<script/{s:cropper/:/cropper/:}' \
		-i "$pkgdir"/etc/smokeping/basepage.html

	chmod 700 "$pkgdir"/etc/smokeping/smokeping_secrets

	# initscript
	install -Dm755 "$srcdir"/smokeping.initd "$pkgdir"/etc/init.d/smokeping

	install -d -o smokeping -g smokeping -m755 \
		"$pkgdir"/var/lib/smokeping \
		"$pkgdir"/var/lib/smokeping/images \
		"$pkgdir"/run/smokeping
}

sha512sums="
6145d2da9c479b379def750ef97167cf8a7cdc8a68337829b8aee5d4303099b0e88f29c485fa22ddf4ee17e44d66b769a1fae49a414068c647cbc27d4d741a1b  smokeping-2.8.2.tar.gz
a1771417350b7e670877646752abdbaec901d6fb5ae360655dbdca2107b69ae48b1c218811fdaf0bc1ca57ea7c956993a8f095ef83e2f079ccb9d65511930caf  smokeping.initd
e6bc389d8edbc000f5ad65848308197b3eb1ddb24bfa9df9c05fa027103952484383b7e021bea86795dceccdcf3f3d51a24c22cae3a68dc5afa30a72b2dbf803  musl-lc_numeric-utf8.patch
fc54c97977b043174ca26a475154c9f819a2d4b257d02c92a8850651c943da041c2e10d337d96e154f3c320102671b688ab2d9cac11393e0510d3c2b1a48c401  smokeping-config.patch
e8dcb91da86b8ff02a264ec3ef564a15492f6d96c2ca9d15fe15dd913c259801c88bdfb03151a557abeea2b775425da9ee23c3e20e478c426c086637ae706763  smokeping-silence.patch
f35c1f793d5765c7060fffd069fb691213f26048308af9bbb908cd6328080f6b1d155bffe60ff0281eb094dbfc28534b37d2ed955f103c9bac91915207c14c58  smokeping-2.8.2-no-3rd-party.patch
acfee65f8b0f66521089ffbd000bf34d7e2c8bf0a23d77b1167288f390e651602621179121573bd0a3dc1129fc9a4dd3aa7c1206500ed92c7825f6625960d60a  smokeping-2.8.2-paths.patch
"
